package com.rongke.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.rongke.enums.OrderStatusEnum;
import com.rongke.mapper.BuyTogetherGoodsMapper;
import com.rongke.mapper.OrderGoodsMapper;
import com.rongke.mapper.OrdersMapper;
import com.rongke.mapper.SystemParmMapper;
import com.rongke.model.*;
import com.rongke.service.MuserService;
import com.rongke.service.OrderGoodsService;
import com.rongke.service.PanicBuyingGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @OrderGoodsServiceImpl
 * @商品订单详情ServiceImpl
 * @version : Ver 1.0
 */
@Service
public class OrderGoodsServiceImpl extends ServiceImpl<OrderGoodsMapper, OrderGoods> implements OrderGoodsService {

    @Autowired
    private OrderGoodsMapper orderGoodsMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private PanicBuyingGoodsService panicBuyingGoodsService;
    @Autowired
    private BuyTogetherGoodsMapper buyTogetherGoodsMapper;
    @Autowired
    private SystemParmMapper systemParmMapper;
    @Autowired
    private MuserService muserService;
    /**
     * @根据条件查找商品订单详情
     * @param orderGoods
     * @return
     */
    @Override
    public List<OrderGoods> selectByCondition(OrderGoods orderGoods) {
        return orderGoodsMapper.selectByCondition(orderGoods);
    }

    @Override
    public List<OrderGoods> selectOrderGoodsByOrderId(Long orderId) {
        return orderGoodsMapper.selectOrderGoodsByOrderId(orderId);
    }

    @Override
    public JSONObject findCashBackYbAndDuoBi(Long orderId,Long userId) {
        JSONObject json=new JSONObject();
        //查询返现手续费
        List<SystemParm> sysparms=systemParmMapper.selectList(null);
        SystemParm sysparm=sysparms.get(0);
        if(sysparm.getCashBackPoundage()==null){
            json.put("code","500");
            json.put("message","未设置返现手续费");
            return json;
        }
        Double returnRate=null,returnRatey=null;
        Double totalReurnRate=0D,totalReturnRatey=0D;
        Orders orders=ordersMapper.selectById(orderId);
        //如果确认收货 返现金额从订单中取

        if(orders.getOrderStatus()== OrderStatusEnum.DELIVERING.getType() || orders.getOrderStatus()==OrderStatusEnum.PENDING_EVALUATION.getType()){
            totalReurnRate=orders.getReturnDuobi().doubleValue();
            totalReturnRatey=orders.getReturnYabi().doubleValue();
            json.put("code","200");
            json.put("totalReurnRate",totalReurnRate);
            json.put("totalReturnRatey",totalReturnRatey);
            return json;
        }
        Integer type=orders.getOrderType();
        List<OrderGoods> orderGoodses=orderGoodsMapper.selectOrderGoodsByOrderId(orderId);
        Muser muser=muserService.selectById(userId);
        String userReturnRate=muserService.getReturnRate(muser.getMemberLevel());//会员等级对应的返现率
        Double cashBackPoundage =sysparm.getCashBackPoundage();
        if(type==1){//0 普通订单 1 拼团订单
            OrderGoods orderGoods=orderGoodses.get(0);
            BuyTogetherGoods buyTogetherGoods=new BuyTogetherGoods();
            buyTogetherGoods.setGoodsId(orderGoods.getGoodsId());
            BuyTogetherGoods btg=buyTogetherGoodsMapper.selectOne(buyTogetherGoods);
            Goods goods=orderGoods.getGoods();
            returnRate=goods.getReturnRate()+ Double.valueOf(userReturnRate);//蜜哆哆返回比率
            returnRatey=goods.getReturnRatey();//米雅阁返回比率
            totalReurnRate+=new BigDecimal(returnRate).multiply(btg.getBuyTogetherPrice()).multiply(new BigDecimal(orderGoods.getSkuCount())).multiply(new BigDecimal(1-cashBackPoundage)).doubleValue();
            totalReturnRatey+=new BigDecimal(returnRatey).multiply(btg.getBuyTogetherPrice()).multiply(new BigDecimal(orderGoods.getSkuCount())).multiply(new BigDecimal(1-cashBackPoundage)).doubleValue();
            json.put("code","200");
            json.put("totalReurnRate",totalReurnRate);
            json.put("totalReturnRatey",totalReturnRatey);
            return json;
        }else if(type==2){//试用众测订单
            json.put("code","200");
            json.put("totalReurnRate",0);
            json.put("totalReturnRatey",0);
            return json;
        }else if(type==3){//今日特卖
            OrderGoods orderGoods=orderGoodses.get(0);
            EntityWrapper<PanicBuyingGoods> ew=new EntityWrapper<>();
            ew.eq("goods_id",orderGoods.getGoodsId());
            PanicBuyingGoods panicBuyingGoods=panicBuyingGoodsService.selectOne(ew);
            returnRate=panicBuyingGoods.getTimeReturnRate().doubleValue();//蜜哆哆返回比率
            for (OrderGoods og:orderGoodses ) {
                Goods goods=og.getGoods();

                Sku sku=og.getSku();
                returnRatey=goods.getReturnRatey();//米雅阁返回比率
                totalReurnRate+=new BigDecimal(returnRate).multiply(sku.getPrice()).multiply(new BigDecimal(og.getSkuCount())).multiply(new BigDecimal(1-cashBackPoundage)).doubleValue();
                totalReturnRatey+=new BigDecimal(returnRatey).multiply(sku.getPrice()).multiply(new BigDecimal(og.getSkuCount())).multiply(new BigDecimal(1-cashBackPoundage)).doubleValue();
            }
            json.put("code","200");
            json.put("totalReurnRate",totalReurnRate);
            json.put("totalReturnRatey",totalReturnRatey);
            return json;
        }else {//0 普通订单
            for (OrderGoods og:orderGoodses ) {
                Goods goods=og.getGoods();
                Sku sku=og.getSku();
                returnRate=goods.getReturnRate()+ Double.valueOf(userReturnRate);//蜜哆哆返回比率
                returnRatey=goods.getReturnRatey();//米雅阁返回比率
                totalReurnRate+=new BigDecimal(returnRate).multiply(sku.getPrice()).multiply(new BigDecimal(og.getSkuCount())).multiply(new BigDecimal(1-cashBackPoundage)).doubleValue();
                totalReturnRatey+=new BigDecimal(returnRatey).multiply(sku.getPrice()).multiply(new BigDecimal(og.getSkuCount())).multiply(new BigDecimal(1-cashBackPoundage)).doubleValue();
            }
            json.put("code","200");
            json.put("totalReurnRate",totalReurnRate);
            json.put("totalReturnRatey",totalReturnRatey);
            return json;
        }
    }

}
